package com.jeethink.web.controller.system;

import java.util.List;
import java.util.Map;

import com.jeethink.system.domain.InfoManagement;
import com.jeethink.system.service.IInfoManagementService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.jeethink.common.annotation.Log;
import com.jeethink.common.enums.BusinessType;
import com.jeethink.system.domain.InfoEvaluate;
import com.jeethink.system.service.IInfoEvaluateService;
import com.jeethink.common.core.controller.BaseController;
import com.jeethink.common.core.domain.AjaxResult;
import com.jeethink.common.utils.poi.ExcelUtil;
import com.jeethink.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;


/**
 * 系统评价Controller
 * 
 * @author jeethink
 * @date 2022-09-19
 */
@Controller
@RequestMapping("/system/evaluate")
public class InfoEvaluateController extends BaseController
{
    private String prefix = "system/evaluate";

    @Autowired
    private IInfoEvaluateService infoEvaluateService;
    @Autowired
    private IInfoManagementService infoManagementService;

    @RequiresPermissions("system:evaluate:view")
    @GetMapping()
    public String evaluate()
    {
        return prefix + "/evaluate";
    }


    @GetMapping("/evaluate")
    public String evaluate2()
    {
        return prefix + "/evaluate";
    }

    @GetMapping("/evaluate/{infoId}")
    public String evaluate2(@PathVariable("infoId")String infoId,Model model)
    {
        model.addAttribute("infoId",infoId);
        return prefix + "/evaluate";
    }
    /**
     * 查询系统评价列表
     */
    @RequiresPermissions("system:evaluate:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(InfoEvaluate infoEvaluate)
    {
        startPage();

        List<InfoEvaluate> list = infoEvaluateService.selectInfoEvaluateList(infoEvaluate);

        for (InfoEvaluate evaluate : list) {
            InfoManagement infoManagement = infoManagementService.selectInfoManagementById(evaluate.getInfoId());
            evaluate.setEnterprise_name(infoManagement.getEnterpriseName());
            evaluate.setSystem_name(evaluate.getSystem_name());
            evaluate.setSystem_type(evaluate.getSystem_type());
        }
        return getDataTable(list);
    }


    @RequestMapping("/list2")
    @ResponseBody
    public TableDataInfo list2( InfoEvaluate infoEvaluate, ModelMap mmap, Model model)
    {


            List<InfoEvaluate> list = infoEvaluateService.selectInfoEvaluateList(infoEvaluate);
            logger.info(infoEvaluate.toString());
        for (InfoEvaluate evaluate : list) {

            model.addAttribute("infoId",evaluate.getInfoId());
            InfoManagement infoManagement = infoManagementService.selectInfoManagementById(evaluate.getInfoId());
            evaluate.setEnterprise_name(infoManagement.getEnterpriseName());
            evaluate.setSystem_name(infoManagement.getSystemName());
            evaluate.setSystem_type(infoManagement.getSystemType());

        }


        mmap.put("infoEvaluate", list);
        return getDataTable(list);
    }

    /**
     * 导出系统评价列表
     */
    @RequiresPermissions("system:evaluate:export")
    @Log(title = "系统评价", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(InfoEvaluate infoEvaluate)
    {
        List<InfoEvaluate> list = infoEvaluateService.selectInfoEvaluateList(infoEvaluate);
        ExcelUtil<InfoEvaluate> util = new ExcelUtil<InfoEvaluate>(InfoEvaluate.class);
        return util.exportExcel(list, "evaluate");
    }

    /**
     * 新增系统评价
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }


    /**
     * 新增系统评价
     */
    @RequestMapping("/add/{infoId}")
    public String add2s(@PathVariable("infoId") String id,Model model)
    {
        logger.error("id:"+id);
        model.addAttribute("infoId",id);
        return prefix + "/add";
    }

    /**
     * 新增系统评价
     */

    /**
     * 新增保存系统评价
     */
    @RequiresPermissions("system:evaluate:add")
    @Log(title = "系统评价", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(InfoEvaluate infoEvaluate, Model model,HttpServletRequest request)
    {


        return toAjax(infoEvaluateService.insertInfoEvaluate(infoEvaluate));
    }

//    @RequiresPermissions("system:evaluate:add")
//    @Log(title = "系统评价", businessType = BusinessType.INSERT)
//    @PostMapping("/add")
//    @ResponseBody
//    public AjaxResult addSave2(@PathVariable("infoId") String id, InfoEvaluate infoEvaluate, ModelMap mmap)
//    {
//
//        logger.error(id);
//
//        infoEvaluate.setInfoId(Long.valueOf(id));
//        return toAjax(infoEvaluateService.insertInfoEvaluate(infoEvaluate));
//    }

    /**
     * 修改系统评价
     */
    @GetMapping("/edit/{evaluateId}")
    public String edit(@PathVariable("evaluateId") Long evaluateId, ModelMap mmap)
    {
        InfoEvaluate infoEvaluate = infoEvaluateService.selectInfoEvaluateById(evaluateId);
        mmap.put("infoEvaluate", infoEvaluate);
        return prefix + "/edit";
    }

    /**
     * 修改保存系统评价
     */
    @RequiresPermissions("system:evaluate:edit")
    @Log(title = "系统评价", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(InfoEvaluate infoEvaluate,ModelMap mmap)
    {
        infoEvaluate.setInfoId((Long) mmap.get("infoId"));
        return toAjax(infoEvaluateService.updateInfoEvaluate(infoEvaluate));
    }

    /**
     * 删除系统评价
     */
    @RequiresPermissions("system:evaluate:remove")
    @Log(title = "系统评价", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(infoEvaluateService.deleteInfoEvaluateByIds(ids));
    }


    @GetMapping("/count")
    @ResponseBody
    public List<Map<String, Object>> count(Model model){
        model.addAttribute("InfoCount",infoEvaluateService.selectInFoEvaluateByCount());
        return infoEvaluateService.selectInFoEvaluateByCount();
    }
}
